import Mock from 'mockjs'

Mock.setup({
    timeout: '200'
})

let { toMoreList } = Mock.mock({
    toMoreList: [
        {
            id: 1,
            name: '节点控制测试',
            number: '00111',
            price: 200,
            audit: true,
            comment: '',
            creator: '超级管理员',
            creationTime: '2022-08-18 01:58:51',
            operation: '审核'
        },
        {
            id: 2,
            name: '花花公子男外套男情侣装冰丝速干皮肤衣上衣',
            number: '10044464880643',
            price: 139,
            audit: false,
            comment: '花花公子专卖店',
            creator: '超级管理员',
            creationTime: '2022-07-04 00:25:29',
            operation: '查看'
        },
        {
            id: 3,
            name: '真维斯短袖t恤男2022春夏季青少年宽松学生百搭',
            number: '10043785248188',
            price: 45,
            audit: false,
            comment: '真维斯韬客专卖店',
            creator: '超级管理员',
            creationTime: '2022-07-04 00:24:09',
            operation: '查看'
        },
        {
            id: 4,
            name: '南极人冰丝短袖t恤男2022夏季滑料冰感半袖体恤',
            number: '10053965809814',
            price: 60,
            audit: false,
            comment: '南极人速麦专卖店',
            creator: '超级管理员',
            creationTime: '2022-07-04 00:23:16',
            operation: '查看'
        }
    ]
})

// 查询全部接口
Mock.mock('zspGetAll', 'get', (option) => {
    return toMoreList
})
// 删除接口
Mock.mock('zspDel', 'post', (option) => {
    let idArr = JSON.parse(option.body).arr.map(r=>r.id)
    let judge = true
   idArr.forEach(r=>{
    if (toMoreList.findIndex(g => g.id==r)==-1) {
        return judge = false
    }else{
        toMoreList.splice(toMoreList.findIndex(g=> g.id==r),1)
        return judge = true
    } 
   })
   return judge
})
// 修改接口
Mock.mock('zspUpDate', 'post', (option) => {
    let stu = toMoreList.find(r => r.id == JSON.parse(option.body).id)
    let obj = JSON.parse(option.body)
    stu.id = obj.id
    stu.name = obj.name
    stu.number = obj.number
    stu.price = obj.price
    stu.audit = obj.audit
    stu.comment = obj.comment
    stu.creator = obj.creator
    stu.creationTime = obj.creationTime
    stu.operation = obj.operation
    return true
})
// 添加接口
Mock.mock('zspGetAdd', 'post', (option) => {
    let stu = JSON.parse(option.body)
    if (toMoreList.some(r => r.id == stu.id)) return false
    else {
        toMoreList.push({
            id: stu.id,
            name : stu.name,
            number : stu.number,
            price : stu.price,
            audit : false,
            comment : stu.comment,
            creator : '超级管理员',
            creationTime : stu.creationTime,
            operation : '查看',
        })
        return true
    }
})
// 分页 模糊查询
Mock.mock(/^GetPageAll/, 'get', (option) => {
    let search = option.url.split('?')[1]
    let params = {}
    search.split('&').forEach(r => {
        let arr = r.split('=')
        params[arr[0]] = arr[1]
    })
    let { currentPage, pageSize, name } = params
    let string = decodeURIComponent(name)
    if (string == '') {
        return {
            data: toMoreList.slice((currentPage - 1) * pageSize, currentPage * pageSize),
            count: toMoreList.length
        }
    } else {
        return {
            data: mohuchaxun1(string, toMoreList).slice((currentPage - 1) * pageSize, currentPage * pageSize),
            count: toMoreList.length
        }
    }
})
// 模糊查询
function mohuchaxun1(name, object) {
    let obj = []
    let a = []
    object.forEach(r => {
        a.push(r.name)
    })
    let arr = [];
    for (var i = 0; i < a.length; i++) {
        if (a[i].indexOf(name) >= 0) {
            arr.push(a[i]);
        }
    }
    arr.forEach(r => {
        obj.push(object.find(s => s.name == r))
    })
    return obj
}
export default Mock